function gotoadd(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_add,"/adminconsole/UserManager/UerLevel/add.jsp","addDialog",AddEventClick,loadElementFinish,null);
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",800);
	loadCounterAndPOS(dialog);
}

function AddEventClick(dialog,id){
	if(dialog.find("#title").val() == "-"){
		return false
		simpleDialog("Please Input Title");
	}
	if(dialog.find("#pointdropdown").val() == "-"){
		return false
		simpleDialog("Please Select Default Area");
	}
	
	waitingDialog({});
	if($("[name=maxEntries]").val() == null){
		var line = 10;
	}else{
		var line = $("[name=maxEntries]").val();
	}
	if($("[name=selectpage]").val() == null){
		var pagePointer = 1;
	}else{
		var pagePointer = $("[name=selectpage]").val();
	}
	
	var title = dialog.find("#title").val();
	var workarea = "";
	for(var i = 0;i<dialog.find("#pointlist [type=checkbox]").size();i++){
		if(dialog.find("#pointlist [type=checkbox]")[i].checked == true){
			workarea += $(dialog.find("#pointlist [type=checkbox]")[i]).val()+",";
		}
	}
	workarea = workarea.substring(0,workarea.length-1);
	var defaultpoint = dialog.find("#pointdropdown").val();
	var canShort = dialog.find("#canShort").is(":checked");
	var adminAllowed = dialog.find("#adminAllowed").is(":checked");
	var housekeepingAllowed = dialog.find("#housekeepingAllowed").is(":checked");
	var url = "/userlevelService";
	if(id == null){
		var func = "addUserLevel";
	}else{
		var func = "editUserLevel"
	}
	var data = { func : func,
			id : id,
			title : title,
			defaultpoint : defaultpoint,
			workarea : workarea,
			canShort : canShort,
			adminAllowed : adminAllowed,
			housekeepingAllowed : housekeepingAllowed,
			offset: (line*pagePointer)-line,
			line: line,
			pagePointer: pagePointer
		}
	simpleservice(url,data,showUserLevelList);
}

function EditDialog(dom,id){
	var dialog = editAdminDataDialog("right",dom,label_add,"/adminconsole/UserManager/UerLevel/add.jsp","addDialog",AddEventClick,loadEditElementFinish,id);
}

function loadEditElementFinish(dialog,id){
	cursorDialogResize(dialog,{width:800});
	EditUserLevel(id,dialog);
}

function EditUserLevel(id,dialog){
	var url = "/userlevelService";
	var data = { func : "getUserLevelById",
			id : id
			}
	simpleserviceSyn(url,data,editSuccess,dialog);
}

function editSuccess(data,dialog){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	setCounterAndPOS(JSON.stringify(objArray2["POS"]),dialog);
	var objArray = objArray2["UserLevel"];
	dialog.find("#title").val(objArray['title']);
	var usedPOS = objArray["workAreasString"].split(",");
	var checkbox = dialog.find("[name=counter]");
	for(var i=0;i<usedPOS.length;i++){
		for(var x=0;x<checkbox.size();x++){
			if(checkbox[x].value == usedPOS[i]){
				$(checkbox[x]).attr("checked",true);
				showinselectdropdown($(checkbox[x]));
				break;
			}
		}
	}
	if(objArray["shortHotelCode"]){
		dialog.find("#canShort").attr("checked",true);
	}
	if(objArray["adminAllowed"]){
		dialog.find("#adminAllowed").attr("checked",true);
	}
	if(objArray["housekeepingAllowed"]){
		dialog.find("#housekeepingAllowed").attr("checked",true);
	}
	dialog.find("#pointdropdown").val(objArray['defaultWorkAreasString']);
}

function showUserLevel(id){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/userlevelService";
	var data = { func: "getAllUserLevel4Admin",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showUserLevelList);
}

function showUserLevelList(data){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = objArray2["pageAmount"];
	var line = objArray2["lineAmount"];
	var pagePointer = objArray2["pageNumber"];
	var objArray = objArray2["data"];
	$("#listsize").html("&nbsp;("+objArray2["dataAmount"]+")");
	var lineNumber = (line * pagePointer) - (line-1) ;
	
	if(objArray.length == 0){
		text = "<tr><td colspan=\"7\" style=\"border-top:1px #D6D7D5 solid;border-bottom:1px #D6D7D5 solid;\">"+nodata+"</td></tr>";
		sumtext += text;
	}else{
		for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					var allareas = "";
					for(var c=0;c<objArray[i]["workAreas"].length;c++){
						allareas += objArray[i]["workAreas"][c]+ "</br>"
					}
					var allareas = allareas.substring(0,allareas.length - 5);
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
							"<td align=\"center\"> "+objArray[i]['title']+" </td>"+
							"<td align=\"center\"> "+allareas+" </td>"+
							"<td align=\"center\"> "+objArray[i]['defaultArea']+" </td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"EditDialog($(this),"+objArray[i]['id']+")\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatus("+objArray[i]['id']+") \"> " +
							"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showSalutation()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showSalutation()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
	}
	$("#showList tr").not($("#showList").find('.tablehead').parent()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showSalutation(id)");
		$("#next").html("<a href=\"#\">"+foor_next+"&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showSalutation(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;"+foor_pre+"</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteUserLevel(){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	if(listCheckBox == ""){
		return false;
	}else{
		var url = "/userlevelService";
		var data = { func : "delUserLevel",
				id : listCheckBox.substring(0,listCheckBox.length - 1),
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
			}
		simpleservice(url,data,showUserLevelList);
	}
}

function changeStatus(id){
	waitingDialog({});
	var url = "/userlevelService";
	var data = { func : "changeStatus",
			id : id
		}
	simpleservice(url,data,changeStatusSuccess);
}

function changeStatusSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function loadCounterAndPOS(dialog){
	var url = "/userlevelService";
	var data = { func : "getPOS"
		}
	simpleserviceSyn(url,data,setCounterAndPOS,dialog);
}

function setCounterAndPOS(data,dialog){
	var objArray1 = new Array();
	objArray1 = JSON.parse(data);
	var text = "<tr>";
	var sumtext = "";
	for(var i=0;i<objArray1.length;i++){
		if(i%2 !=0 && i != objArray1.length-1){
			text += "<td style=\"width:3%\"><input type=\"checkbox\" name=\"counter\" title=\""+objArray1[i]['title']+"\" value=\""+objArray1[i]['id']+"\" onclick=\"showinselectdropdown($(this))\"></td>"+
			"<td>"+objArray1[i]['title']+"</td>";
		}else if(i%2 == 0 && i != objArray1.length-1){
			text += "</tr><tr><td style=\"width:3%\"><input type=\"checkbox\" name=\"counter\" title=\""+objArray1[i]['title']+"\" value=\""+objArray1[i]['id']+"\" onclick=\"showinselectdropdown($(this))\"></td>"+
			"<td>"+objArray1[i]['title']+"</td>";
		}else if(i%2 != 0 && i == objArray1.length-1){
			text += "<td style=\"width:3%\"><input type=\"checkbox\" name=\"counter\" title=\""+objArray1[i]['title']+"\" value=\""+objArray1[i]['id']+"\" onclick=\"showinselectdropdown($(this))\"></td>"+
			"<td>"+objArray1[i]['title']+"</td></tr>";
		}else if(i%2 == 0 && i == objArray1.length-1){
			text += "</tr><tr><td style=\"width:3%\"><input type=\"checkbox\" name=\"counter\" title=\""+objArray1[i]['title']+"\" value=\""+objArray1[i]['id']+"\" onclick=\"showinselectdropdown($(this))\"></td>"+
			"<td>"+objArray1[i]['title']+"</td></tr>";
		}
		
	}
	sumtext += text;
	dialog.find("#pointlist").append(sumtext);
}

function showinselectdropdown(dom){
	var dialog = dom.parents("#BlankDialog");
	var value = dom.val();
	var text = dom.attr("title");
	if(dom.is(":checked")){
		var have = false;
		for(var i=0;i<dialog.find("#pointdropdown option").size();i++){
			if(dialog.find("#pointdropdown option")[i].value == value){
				have = true;
				break;
			}
		}
		if(!have){
			dialog.find("#pointdropdown").append($("<option>").val(value).html(text));
			dialog.find("#allpoint").append($("<option>").val(value).html(text));
		}
	}else{
		for(var i=0;i<dialog.find("#pointdropdown option").size();i++){
			if(dialog.find("#pointdropdown option")[i].value == value){
				dialog.find("#pointdropdown option")[i].remove();
				break;
			}
		}
	}
}

function movePoint(SS1,SS2)
{
    var SelID='';
    var SelText='';
    SS1 = SS1[1];
    SS2 = SS2[1];
    // Move rows from SS1 to SS2 from bottom to top
    
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}
function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}